package com.bluemson.controller;

import com.bluemson.entity.Article;
import com.bluemson.entity.Comment;
import com.bluemson.entity.Plate;
import com.bluemson.entity.User;
import com.bluemson.service.AddService;
import com.bluemson.service.DeleteService;
import com.bluemson.service.GetService;
import com.bluemson.service.Impl.AddServiceImpl;
import com.bluemson.service.Impl.DeleteServiceImpl;
import com.bluemson.service.Impl.GetServiceImpl;
import com.bluemson.service.Impl.UpdateServiceImpl;
import com.bluemson.service.UpdateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage")
public class ManagerController {
    @RequestMapping(value = {"/addPlate"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map addPlate(@RequestBody Plate plate){
        AddService addService = new AddServiceImpl();
        Map map = new HashMap();
        if(addService.addPlate(plate)){
            map.put("msg","添加成功");
            map.put("status",1);
        }else {
            map.put("msg","添加失败,已存在该板块或者该用户已是该板块的版主");
            map.put("status",0);
        }
        return map;
    }
    @RequestMapping(value = {"/addCover"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map addCover(@RequestParam("cover") MultipartFile cover, @RequestParam("name")String name, HttpServletRequest request) throws IOException {
        AddService addService = new AddServiceImpl();
        Map map = new HashMap();
        String uri = addService.addCover(cover,request,name);
        map.put("msg","添加封面成功");
        map.put("uri",uri);
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/deletePlate"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map deletePlate(@RequestBody Plate plate){
        DeleteService deleteService = new DeleteServiceImpl();
        Map map = new HashMap();
        deleteService.deletePlate(plate);
        map.put("msg","板块删除成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/updatePlate"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map updatePlate(@RequestBody Plate plate){
        UpdateService updateService = new UpdateServiceImpl();
        Map map = new HashMap();
        if(updateService.updatePlate(plate)){
            map.put("msg","板块更新成功");
            map.put("status",1);
        }else {
            map.put("msg","已存在同名板块");
            map.put("status",0);
        }
        return map;
    }
    @RequestMapping(value = {"/deleteHost"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map deleteHost(@RequestBody Plate plate){
        DeleteService deleteService = new DeleteServiceImpl();
        deleteService.deleteHost(plate);
        Map map = new HashMap();
        map.put("msg","删除版主成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/moveArticle"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map moveArticle(@RequestBody Article article) {
        UpdateService updateService = new UpdateServiceImpl();
        updateService.moveArticle(article);
        Map map = new HashMap();
        map.put("msg", "移动板块成功");
        map.put("status", 1);
        return map;
    }
    @RequestMapping(value = {"/deleteArticle"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map deleteArticle(@RequestBody Article article){
        DeleteService deleteService = new DeleteServiceImpl();
        deleteService.deleteArticle(article.getId());
        Map map = new HashMap();
        map.put("msg","删除文章成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/topArticle"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map topArticle(@RequestBody Article article){
        UpdateService updateService = new UpdateServiceImpl();
        updateService.topArticle(article.getId(),1);
        Map map = new HashMap();
        map.put("msg","置顶成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/noTopArticle"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map noTopArticle(@RequestBody Article article){
        UpdateService updateService = new UpdateServiceImpl();
        updateService.topArticle(article.getId(),0);
        Map map = new HashMap();
        map.put("msg","取消置顶成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/deleteComment"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map deleteComment(@RequestBody Comment comment){
        DeleteService deleteService = new DeleteServiceImpl();
        deleteService.deleteComment(comment);
        Map map = new HashMap();
        map.put("msg","删除成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = "/searchUser", method = {RequestMethod.GET})
    @ResponseBody
    public Map searchUser(@RequestParam("username")String username){
        GetService getService = new GetServiceImpl();
        List<User> users = getService.searchUser(username);
        Map map= new HashMap();
        map.put("msg","这是所有含该字段的用户");
        map.put("users",users);
        map.put("status",1);
        return map;
    }
}
